//
//  LiShiMapController.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/10/20.
//

import UIKit

class LiShiMapController: SecSuperController,MKMapViewDelegate {
    var dataArray = [[String:Any]]()
    var appleMapView:MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.appleMapView = MKMapView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.appleMapView.layer.masksToBounds = true
        self.appleMapView.layer.cornerRadius = pixw(p: 20)
        self.appleMapView.delegate = self
        self.appleMapView.showsScale = true
        self.appleMapView.showsCompass = false
        self.appleMapView.mapType = .standard
        self.appleMapView.showsUserLocation = true
        self.view.addSubview(self.appleMapView)
        for dic in dataArray {
            //地震经纬度
            let dzLat = Double(dic["EPI_LAT"] as! String) ?? 0.00
            let dzLon = Double(dic["EPI_LON"] as! String) ?? 0.00
            let location = CLLocationCoordinate2D(latitude: dzLat, longitude: dzLon)
            let pointAnnotation = MKPointAnnotation()
                           pointAnnotation.coordinate = location
                           pointAnnotation.title = "\(dic["M"] as? String ?? "")级"
                           pointAnnotation.subtitle = dic["LOCATION_C"] as? String ?? ""
                           self.appleMapView.addAnnotation(pointAnnotation)
                           self.appleMapView.selectAnnotation(pointAnnotation, animated: true)
            // 设置圆形范围
            let shendu = (Double(dic["EPI_DEPTH"] as? String ?? "00") ?? 0.00) * 1000
            let circle = MKCircle(center: location, radius: shendu)
            self.appleMapView.addOverlay(circle)
        }
        
        // 设置地图显示的中心点
        let center = CLLocationCoordinate2D(latitude: MYGlobal.share().locationModel.lat, longitude: MYGlobal.share().locationModel.lon)

        // 设置地图的跨度，控制缩放比例
        let span = MKCoordinateSpan(latitudeDelta: 150, longitudeDelta: 150)

        // 创建 MKCoordinateRegion 实例
        let region = MKCoordinateRegion(center: center, span: span)

        // 设置地图的显示区域
        appleMapView.setRegion(region, animated: true)
        self.appleMapView.centerCoordinate = center
        addbulrEffectView(superView: self.appleMapView)
    }
}
extension LiShiMapController{
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKPointAnnotation{
            let identifier = "CustomAnnotationView"
                   var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
                   if annotationView == nil {
                       annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                       annotationView?.canShowCallout = true

                       // 创建一个自定义按钮作为右侧的附属视图
                       let rightButton = UIButton(type:.detailDisclosure)
                       rightButton.setImage(UIImage(named: "comm_right"), for: .normal)
                       annotationView?.rightCalloutAccessoryView = rightButton
                   } else {
                       annotationView?.annotation = annotation
                   }
                   return annotationView
            
        }
        return nil
    }

    
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        DLog(message: "走这里")
        if view.annotation  is MKPointAnnotation{
            if MYGlobal.share().isVip() == false {
                Helper.alertShow(withTitle: LocalizedString("提示"), withContent: LocalizedString("您不是VIP会员，暂时无法使用该功能！点击去开通解锁该功能！"), withSureBtnTitle: LocalizedString("去开通"), withCancelBtnTitle: LocalizedString("取消")) { index in
                    //去往购买中心
                    if index == 1 {
                        let controller = PayMarkController()
                        self.navigationController?.pushViewController(controller, animated: true)
                    }
                }
                return
            }
            let title = view.annotation!.title
            let subTitle = view.annotation!.subtitle
            for dic in self.dataArray {
                let tempTitle = "\(dic["M"] ?? "")级"
                let tempSubTitle = "\(dic["LOCATION_C"] ?? "")"
                if title == tempTitle && tempSubTitle == subTitle {
                    let controller = MapDetailController()
                    controller.dic = dic
                    self.navigationController?.pushViewController(controller, animated: true)
                    return
                }
            }
        }
    }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay is MKCircle{
            let cirleRender = MKCircleRenderer(overlay: overlay)
            cirleRender.lineWidth = 2
            cirleRender.strokeColor = MYColor.color(withHexString: "#E04148", alpha: 0.2)
            cirleRender.fillColor = MYColor.color(withHexString: "#999999", alpha: 0.2)
            return cirleRender
        }
        return MKOverlayRenderer()
    }
}

